from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'


N = 'google_gemini-2.5-flash-preview-05-20-thinking_0613-172953'
p: Path = Path.cwd().parent / 'ixc-qa'


from pathlib import Path
import json
idx = p / 'index.json'
if not idx.exists():
    raise FileNotFoundError(f"Index file {idx} not found.")
Idx = json.loads(idx.read_text())

# 2. find the session by name
o = [ i for i in Idx if i['name'] == N ]
if not o:
    raise ValueError(f"Session {N} not found in index.")
o = o[0]

# 3.find the files
l = sorted(p.glob(f'{N}-*.md'))
l


# 4. start the session
from u import Sess, M
k = json.loads(Path("/home/me/.ssh/k-250611-h3c.json").read_text())
key = k['key']  # Replace
think_chunk_mode = False

s = Sess(name=o['name'], model=o['model'], url=o['url'], key=key, think_chunk_mode=think_chunk_mode)

# 4.1 load the messages
for m in l:
    if m.is_file():
        r = m.stem.split('-')[-1]
        if r == 'reasoning':
            continue            # 🦜 : skip this
        print(f"Loading {m} with role {r}")
        s.ms.append(M(role=r, content=m.read_text()))
s

# 4.2 chat more
s.add_q('''
Thanks. That's so helpful.
'''); await s.ask_for_a(); None

# 5. save again
s.name = o['name']
s.dump_to_db(p)


# --------------------------------------------------
# 🦜 : I just realize that it's better to store kv index.json....
Idx1 = {}
for x in Idx:
    n = x['name']
    del x['name']
    Idx1[n] = x
Idx1
idx.write_text(json.dumps(Idx1, indent=4))

# --------------------------------------------------

N = 'openai_gpt-4o-mini_0612-172336'
from pathlib import Path
import json
from u import Sess, M
k = json.loads(Path("/home/me/.ssh/k-250611-h3c.json").read_text())
s = Sess.load(N , k['key'], Path.cwd().parent / 'ixc-qa')

s.add_q('''
How are you today
'''); await s.ask_for_a(); None
s.dump_to_db(Path.cwd().parent / 'ixc-qa')
